﻿namespace DomainServices.Tools.SharedTypes
{
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Threading;

    internal class FilenameMap
    {
        private int _currentId = -1;
        private Dictionary<int, string> _filesById = new Dictionary<int, string>();
        private ConcurrentDictionary<string, int> _idsByFile = new ConcurrentDictionary<string, int>(StringComparer.OrdinalIgnoreCase);
        internal const int NotAFile = 0;

        internal FilenameMap()
        {
            this.AddOrGet(string.Empty);
        }

        internal int AddOrGet(string fileName)
        {
            if (fileName == null)
            {
                fileName = string.Empty;
            }
            return this._idsByFile.GetOrAdd(fileName, delegate (string f) {
                int num = Interlocked.Increment(ref this._currentId);
                this._filesById[num] = f;
                return num;
            });
        }

        internal int this[string fileName]
        {
            get
            {
                if (fileName == null)
                {
                    fileName = string.Empty;
                }
                int num = 0;
                this._idsByFile.TryGetValue(fileName, out num);
                return num;
            }
        }

        internal string this[int id]
        {
            get
            {
                string str = null;
                this._filesById.TryGetValue(id, out str);
                return str;
            }
        }
    }
}

